package com.itheima.health.controller;

import com.itheima.health.common.R;
import com.itheima.health.common.ResultPageData;
import com.itheima.health.entity.TCheckgroup;
import com.itheima.health.entity.TSetmeal;
import com.itheima.health.service.ITSetmealService;
import com.itheima.health.utils.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cpanda
 * @since 2024-01-26
 */
@RestController
@RequestMapping()
public class TSetmealController {
    @Autowired
    private ITSetmealService setmealService;
    /**
     * 阿里 七牛上传
     */
//    @Autowired
//    private AliOSSUtils aliOSSUtils;

    /**
     * 套餐列表显示
     */
    @PostMapping("/setmeal/findPage")
    public ResultPageData findPage(@RequestBody QueryPageBean dto) {
        return setmealService.findPage(dto);
    }

    /**
     * 检查组展示
     */
    @GetMapping("/checkgroup/findAll")
    public R checkgroupfindAll() {
        List<TCheckgroup> tCheckgroup = setmealService.checkgroupfindAll();
        return R.success(tCheckgroup);
    }

    /**
     * 图片上传
     */
    @PostMapping("/setmeal/upload")
    public R setmealUpload(@RequestParam("imgFile") MultipartFile imgFile) throws IOException {
        String upload = setmealService.upload(imgFile);
        return R.success(upload);
    }

    @PostMapping("/setmeal/add")//?checkgroupIds=5,6,7
    public R addSetmeal(@RequestBody TSetmeal setmeal, @RequestParam("checkgroupIds") List<Integer> checkgroupIds) {
        setmealService.addSetmeal(setmeal, checkgroupIds);
        return R.success(null);
    }

    /**
     * 根据id套餐信息回显
     */
    @GetMapping("/setmeal/findById")
    public R setmealById(Integer id) {
        TSetmeal setmeal = setmealService.setmealById(id);
        return R.success(setmeal);
    }

    /**
     * 根据id回显套餐关联检查组信息
     */
    @GetMapping("/checkgroup/findcheckgroupIdsBySetmealId")
    public R findcheckgroupIdsBySetmealId(@RequestParam("setmealId") Integer setmealId) {
        List<Integer> checkGroupList = setmealService.findCheckGroupIdsBySetmealId(setmealId);
        return R.success(checkGroupList);
    }

    /**
     * 修改套餐
     */
    @PutMapping("/setmeal/edit")
    public R updateSetmeal(@RequestBody TSetmeal setmeal, @RequestParam("checkgroupIds") List<Integer> checkgroupIds) {
        setmealService.updateSetmeal(setmeal, checkgroupIds);
        return R.success(null);
    }

    /**
     * 根据id删除套餐
     */
    @DeleteMapping("/setmeal/delete")
    public R deleteSetmeal(Integer id) {
        setmealService.deleteSetmeal(id);
        return R.success(null);
    }
}
